import React, { Component } from 'react';

export default class StateDemo extends Component {
    state={
        count:0
    };
    addCount=()=>{
        // 对象式setState
        // const {count} = this.state;
        // this.setState({
        //     count:count+1
        // },()=>{
        //     console.log("-----",this.state.count);
        // });
        // console.log("======",this.state.count);

        // 函数式setState
        this.setState((state,props)=>{
            // console.log(state,props);
            return {
                count:state.count+1
            };
        },()=>{
            console.log("-----",this.state.count);
        });
        console.log("======",this.state.count);
    };
    render() {
        return (
            <div>
                <h1>当前的求和为：{this.state.count}</h1>
                <br />
                <button onClick={this.addCount}>点我加1</button>
            </div>
        );
    };
};
